package com.villaday.idf.wxapi;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.villaday.idf.R;
import com.villaday.idf.activity.MyOrderDetailActivity;
import com.villaday.idf.activity.OrderDetailsActivity;
import com.villaday.idf.activity.PayOrderActivity;
import com.villaday.idf.activity.UpgradeVipPaymentActivity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.villaday.idf.util.ToastUtil;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Constant.ID.WeChat_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            Log.e("logP", "onPayFinish,Code=" + resp.errCode);
            if (resp.errCode == 0) {//成功
                if ("1".equals(SharedPreferencesUtil.getWechatPayType(this))) {

                    SharedPreferencesUtil.saveWechatStatus(this, true);

                } else {
                    if (SharedPreferencesUtil.getWeOrderBoolean(this)) {
                        Intent intent = new Intent(this, OrderDetailsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("orderNo",SharedPreferencesUtil.getWeOrderNum(WXPayEntryActivity.this));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    } else {
                        SharedPreferencesUtil.saveWechatStatusForOrder(this, true);
                        Intent intent = new Intent(this, MyOrderDetailActivity.class);
                        intent.putExtra("orderNo", SharedPreferencesUtil.getWeOrderNum(WXPayEntryActivity.this));
                        startActivity(intent);
                    }
                }
            } else if (resp.errCode == -2 || resp.errCode == -1) {//失败

                if ("1".equals(SharedPreferencesUtil.getWechatPayType(this))) {


                } else {
                    if (SharedPreferencesUtil.getWeOrderBoolean(this)) {
                        Intent intent = new Intent(this, OrderDetailsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("orderNo",SharedPreferencesUtil.getWeOrderNum(WXPayEntryActivity.this));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    } else {
                        Intent intent = new Intent(this, MyOrderDetailActivity.class);
                        intent.putExtra("orderNo", SharedPreferencesUtil.getWeOrderNum(WXPayEntryActivity.this));
                        startActivity(intent);
                    }
                }

            } else {//用户取消
                if (SharedPreferencesUtil.getWeOrderBoolean(this)) {
                    Intent intent = new Intent(this, OrderDetailsActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("orderNo",SharedPreferencesUtil.getWeOrderNum(WXPayEntryActivity.this));
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
            SharedPreferencesUtil.saveWeOrderBoolean(this,false);




           /* SharedPreferences weChatPay = getSharedPreferences("weChatPay", MODE_PRIVATE);
            String type = weChatPay.getString("PayType", null);
            Intent intent = new Intent(this, PayResultActivity.class);
            intent.putExtra("payMode", "weChat");
            intent.putExtra("result", String.valueOf(resp.errCode));
            intent.putExtra("isCooper", weChatPay.getBoolean("isCooper", false));
            if ("pay".equals(type)) {
                intent.putExtra("type", 0);
            } else {
                intent.putExtra("type", 1);
            }
            startActivity(intent);*/
            finish();
        }
    }
}
